<?php

namespace app\common\model;

use think\Model;


class Download extends Model
{

    // 表名
    protected $name = 'zz_download';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'status_text',
        'category_text',
        'createtime_text',
        'updatetime_text',
    ];

    public function getCreatetimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['createtime']) ? $data['createtime'] : '');
        return $value?date('Y-m-d H:i:s', $value):'';
    }


    public function getUpdatetimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['updatetime']) ? $data['updatetime'] : '');
        return $value?date('Y-m-d H:i:s', $value):'';
    }

    
    public function getStatusList()
    {
        return ['normal' => __('Normal'), 'hidden' => __('Hidden')];
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getCategoryTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['category_id']) ? $data['category_id'] : '');
        $obj = Category::get(['id' => $value]);
        return $obj ? $obj['name'] : '';
    }


}
